package com.ruoyi.mentor.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.LogUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

/**
 * @Author LH
 * @Description
 * @Date Create in 20:18 2021/6/1
 */
@Service
public class MailServiceImpl {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String sendMailUsername;

    @Autowired
    private StringRedisTemplate redisTemplate;


    private boolean regex(String email){
        if (email==null)return false;
        Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
        return pattern.matcher(email).find();
    }

    @Async("threadPoolExecutor")
    public void sendExamineSuccessMail(String toMail){
//        if (!regex(toMail)){
//            return;
//        }
//            SimpleMailMessage message = new SimpleMailMessage();
//            message.setFrom(sendMailUsername);
//            message.setSubject("导师系统审核信息");
//            message.setText("你的个人信息已通过审核,请登录系统查看");
//            message.setTo(toMail);
//            message.setSentDate(new Date());
//            javaMailSender.send(message);

    }
    @Async("threadPoolExecutor")
    public void sendExamineFailMail(String toMail,String reason){
        /*if (!regex(toMail)){
            return;
        }
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sendMailUsername);
        message.setSubject("导师系统审核信息");
        message.setText("你的个人信息未通过审核,请登录系统查看,原因:"+reason);
        message.setTo(toMail);
        message.setSentDate(new Date());
        javaMailSender.send(message);*/
    }
    @Async("threadPoolExecutor")
    public void sendChooseSuccessMail(String toMail){
//        if (!regex(toMail)){
//            return;
//        }
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setFrom(sendMailUsername);
//        message.setSubject("导师系统选择信息");
//        message.setText("导师已确认选择,请登录系统查看");
//        message.setSentDate(new Date());
//        message.setTo(toMail);
//        javaMailSender.send(message);
    }
    @Async("threadPoolExecutor")
    public void sendChooseFailMail(String toMail){
//        if (!regex(toMail)){
//            return;
//        }
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setFrom(sendMailUsername);
//        message.setSubject("导师系统选择信息");
//        message.setText("导师拒绝了你的选择,请登录系统查看");
//        message.setSentDate(new Date());
//        message.setTo(toMail);
//        javaMailSender.send(message);
    }

    @Async("threadPoolExecutor")
    public void sendResetPasswordMail(String toMail){
        if (!regex(toMail)){
            return;
        }

        String checkCode = IdUtils.fastSimpleUUID().substring(0, 8);
        String key = Constants.RESET_PASSWORD_KEY +toMail;

        redisTemplate.opsForValue().set(key,checkCode, 5,TimeUnit.MINUTES);

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sendMailUsername);
        message.setSubject("双选系统|用户密码重置");
        message.setText("您已经请求了重置密码，重置密码的校验码为:"+checkCode+",五分钟内有效");
        message.setSentDate(new Date());
        message.setTo(toMail);
        javaMailSender.send(message);
    }
}
